﻿namespace com.stifel.BranchMgmt
{
    using System.Linq;
    using System.Reflection;

    using com.stifel.BranchMgmtDao;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public class StifelTelephoneSystem_Definition : StifelBaseDefinition
    {
        #region Fields

        const string SITECOLUMNS = "Stifel-TelephoneSystem.xml";

        #endregion Fields

        #region Constructors

        public StifelTelephoneSystem_Definition( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~StifelTelephoneSystem_Definition( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateSiteColumns( GetFieldDefinitionsAsXml( SITECOLUMNS ) );
            CreateContentType( getContentTypeDefn( ) );
            CreateList( getListDefn( ) );
            addEventReceiver( );
        }

        public override void Delete( )
        {
            base.Delete( );
            deleteEventReceiver( );
            DeleteList( getListDefn( ), false );
            DeleteContentType( getContentTypeDefn( ) );
            DeleteSiteColumns( GetFieldDefinitionsAsXml( SITECOLUMNS ) );
        }

        void addEventReceiver( )
        {
            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                SPList spList = spWeb.Lists.GetListByRootFolder( StifelTelephoneSystemDao.ListName );
                if ( spList != null )
                {
                    string assemblyFQN = Assembly.GetExecutingAssembly( ).FullName;
                    string classFQN = typeof( StifelTelephoneSystemEventReceiver ).FullName;
                    spList.EventReceivers.Add( SPEventReceiverType.ItemDeleting, assemblyFQN, classFQN );
                    spList.EventReceivers.Add( SPEventReceiverType.ItemUpdated, assemblyFQN, classFQN );
                }
            }
        }

        void deleteEventReceiver( )
        {
            using ( SPWeb spWeb = Properties.GetWeb( ) )
            {
                SPList spList = spWeb.Lists.GetListByRootFolder( StifelTelephoneSystemDao.ListName );
                if ( spList != null )
                {
                    string assemblyFQN = Assembly.GetExecutingAssembly( ).FullName;
                    string classFQN = typeof( StifelTelephoneSystemEventReceiver ).FullName;
                    for ( int i = spList.EventReceivers.Count - 1; i >= 0; i-- )
                    {
                        if ( spList.EventReceivers[ i ].Assembly.Equals( assemblyFQN ) && spList.EventReceivers[ i ].Class.Equals( classFQN ) )
                        {
                            if ( spList.EventReceivers[ i ].Type.Equals( SPEventReceiverType.ItemUpdated ) )
                                spList.EventReceivers[ i ].Delete( );
                            else if ( spList.EventReceivers[ i ].Type.Equals( SPEventReceiverType.ItemDeleting ) )
                                spList.EventReceivers[ i ].Delete( );
                        }
                    }
                }
            }
        }

        ContentTypeDefn getContentTypeDefn( )
        {
            StifelTelephoneSystem c = null;
            return new ContentTypeDefn
            {
                Name = Item.GetContentTypeName<StifelTelephoneSystem>( ),
                ContentTypeId = Item.GetContentTypeId<StifelTelephoneSystem>( ),
                WebLevel = true,
                Group = "Stifel",
                Description = "Describes telephone equipment.",
                Fields = new FieldDefn[ ] {
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelTelephoneSystem>(c.GetPropertySymbol(a=>a.BranchLookupId)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelTelephoneSystem>(c.GetPropertySymbol(a=>a.TelephoneMaintenanceContract))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelTelephoneSystem>(c.GetPropertySymbol(a=>a.ContractExpiration))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelTelephoneSystem>(c.GetPropertySymbol(a=>a.TelephoneCircuitIds))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelTelephoneSystem>(c.GetPropertySymbol(a=>a.TelephoneEquipment))
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelTelephoneSystem>(c.GetPropertySymbol(a=>a.TelephoneEquipmentNotification)),
                        Hidden=true
                    },
                }.Distinct( ).ToList( )
            };
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = StifelTelephoneSystemDao.ListName,
                Title = "Stifel Telephone Equipment",
                Description = "Contains telephone equipment.",
                ContentTypes = new string[ ] {
                    Item.GetContentTypeName<StifelTelephoneSystem>()
                }.ToList( )
            };
        }

        #endregion Methods
    }
}